using System.Security.Principal;
using AndyPike.Commons.TestSupport;
using AndyPike.Commons.Web;
using AndyPike.Snowflake.Core.Services;
using AndyPike.Snowflake.Web.Controllers;
using AndyPike.Snowflake.Web.Filters;
using NUnit.Framework;
using NUnit.Framework.SyntaxHelpers;
using Rhino.Mocks;

namespace AndyPike.Snowflake.Web.Tests.Unit.Filters
{
    [TestFixture]
    public class When_calling_an_action_that_requires_authentication : FilterUnitTestBase
    {
        private MockRepository mockery;
        private IAccountService accountService;

        [SetUp]
        public void Setup()
        {
            mockery = new MockRepository();
            accountService = mockery.DynamicMock<IAccountService>();

            filter = new AuthenticationFilter();

            controller = new MyController(accountService);
            PrepareController(controller, "My");
        }

        [Test]
        public void Should_allow_execution_to_continue_if_a_principle_has_been_loaded()
        {
            var principal = mockery.Stub<IPrincipal>();
            Context.CurrentUser = principal;

            bool continueExecution = ExecuteFilter();

            Assert.That(continueExecution, Is.True);
        }

        [Test]
        public void Should_redirect_to_the_homepage_if_there_is_no_principle_loaded()
        {
            Context.CurrentUser = null;

            Assert.That(ExecuteFilter(), Is.False);
            Assert.That(Response.RedirectedTo, Is.EqualTo(@"/Home/Index.rails"));
        }
    }
}